//
//  UITableView+AssistView.m
//  BorrowDrawingBook
//
//  Created by QA on 16/4/13.
//  Copyright © 2016年 QA. All rights reserved.
//

#import "UIView+AssistView.h"
#import "RTSpinKitView.h"

/*****自定义内部类******/
@interface UICustomView : UIView
@property (nonatomic, strong) id object;
@end
@implementation UICustomView @end


@implementation UIView (AssistView)

#pragma mark 进度条设置

- (void)showViewProgressRect:(CGRect)frame
{
    //移除其他页面
    [self removeAllCustomFromSuperview];
    frame.size.width = SuperWidth;
    
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.tag = -1201900;
    //view.backgroundColor = [UIColor redColor];
    [self addSubview:view];
    
    //添加进度条
    RTSpinKitView *spinner = [[RTSpinKitView alloc] initWithStyle:RTSpinKitViewStyleFadingCircleAlt];
    spinner.center = CGPointMake(CGRectGetMidX(view.bounds) - 5, CGRectGetMidY(view.bounds));
    [spinner startAnimating];
    [view addSubview:spinner];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, 62, 26)];
    lable.center = CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds) + 42);
    lable.textAlignment = NSTextAlignmentCenter;
    lable.font = [UIFont systemFontOfSize:12];
    lable.textColor = [UIColor grayColor];
    lable.text = @"努力加载中";
    [view addSubview:lable];
}

- (void)showViewProgress
{
    //移除其他页面
    [self removeAllCustomFromSuperview];
    CGRect frame = self.frame;
    frame.size.width = SuperWidth;
    
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.tag = -1201900;
    //view.backgroundColor = [UIColor redColor];
    [self addSubview:view];
    
    //添加进度条
    RTSpinKitView *spinner = [[RTSpinKitView alloc] initWithStyle:RTSpinKitViewStyleFadingCircleAlt];
    spinner.center = CGPointMake(CGRectGetMidX(view.frame), CGRectGetMidY(view.frame)- spinner.frame.size.height/2);
    [spinner startAnimating];
    [view addSubview:spinner];
}

- (void)removeProgressFromSuperview
{
    //移除进度条
    NSArray *viewArray = self.subviews;
    for (id view in viewArray) {
        if ([view isKindOfClass:[UIView class]]) {
            //RTSpinKitView *mSpinKitView = view;
            //[mSpinKitView removeFromSuperview];
            UIView *customView = view;
            if(customView.tag == -1201900)
            [customView removeFromSuperview];
        }
    }
}

#pragma mark 错误页面设置

- (void)showPromptViewText:(NSString*)text rect:(CGRect)frame blcok:(RetryBlock)retryBlcok
{
    //移除其他页面
    [self removeAllCustomFromSuperview];
    
    UICustomView *view = [[UICustomView alloc] initWithFrame:frame];
    view.tag = -1201901;
    view.object = retryBlcok;
    [self addSubview:view];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapBtnView:)];
    [view addGestureRecognizer:tap];
    
    UIImage *image = [UIImage imageNamed:@"guess_like_img"];
    CGFloat imageHeight = image.size.height;
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.center = CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds));
    [view addSubview:imageView];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 26)];
    lable.center = CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds) + imageHeight/2 + 14);
    lable.textAlignment = NSTextAlignmentCenter;
    lable.font = [UIFont systemFontOfSize:14];
    lable.textColor = [UIColor grayColor];
    lable.text = text;
    [view addSubview:lable];
}

- (void)removePromptFromSuperview
{
    //移除进度条
    NSArray *viewArray = self.subviews;
    for (id view in viewArray) {
        if ([view isKindOfClass:[UIView class]]) {
            UIView *customView = view;
            if(customView.tag == -1201901)
                [customView removeFromSuperview];
        }
    }
}

#pragma mark 移除所有页面

- (void)removeAllCustomFromSuperview
{
    [self removeProgressFromSuperview];
    [self removePromptFromSuperview];
}

#pragma mark Block && UITapGestureRecognizernt

- (void)OnTapBtnView:(UITapGestureRecognizer *)sender
{
    RetryBlock myRetryBlock = ((UICustomView*)sender.view).object;
    if(myRetryBlock) myRetryBlock();
}

@end
